/*
*wj：20180822 按b0002协议进行put get编码
*   Get: 获取数据
*   Put：编辑数据
*/

<template>
    <div class="sys-page">
        <app-title title="终端登陆失败次数阈值配置"  style="display:inline;"></app-title>
<el-button type="success" @click="refresh()" round style="float:right; margin-top:10px;" icon="el-icon-refresh">刷新</el-button>
            <el-table v-loading="loading" border :data="tableData.body" :row-class-name="tableRowClassName">
                <el-table-column prop="loginFailCnt" :label="tableDesc" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" size="small" @click="edit">修改</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-dialog :title="dialogTitle" :visible.sync="editTableShow" width='30%' :fullscreen='false'>
                <el-form :rules="rules">
                    <el-form-item prop="inputVal" label="失败次数阈值">
                <el-select v-model="inputVal" placeholder="1">
                    <el-option
                    v-for="item in 30"
                    :key="item"
                    :label="item"
                    :value="item">
                    </el-option>
                </el-select>   
                  </el-form-item>
                </el-form>
                <el-button @click.native.prevent="ok">确定</el-button>
                <el-button @click.native.prevent="cancel">取消</el-button>

            </el-dialog>
    </div>

</template>
<style>
  .el-table .warning-row {
    background: #F08080;
  }

  .el-table .success-row {
    background:#f0f9eb;
  }
</style>

<script>
export default {
    name:'terminalloginfailedpolicy',
    data(){
        var validNumber= (rule, val, callback) => {
                console.log("in invalidNum "+val)
                var value = this.inputVal
                var pattern = /^[1-9]\d*$/
                if(value === ''){
                    this.okEnable = true;
                    callback(new Error("请输入有效期值"))
                }else if(pattern.test(value) == false){
                    this.okEnable = true;
                    callback(new Error("请输入数字"))
                }else {
                    this.okEnable = false;
                    callback()
                }
        }
        return {
            rules: {
                inputVal: [{ validator: validNumber, trigger: 'change', required: true, message:"请输入数字"}]
            },
            dialogTitle:"终端登陆失败次数阈值配置",
            tableDesc : '',
            loading: false,
            editTableShow: false,
            inputVal:1,
            tableData:{
                body:[{loginFailCnt: ''}],
                head:[{key:''}],
                loading:true
            },
            errorMsg:"数据被异常篡改，请联系管理员",
            issafe:true

       }
    },
    mounted(){
        this.query()
    },
    methods:{
        refresh(){
            this.query()
        },
        tableRowClassName({row, rowIndex}) {
          if(this.issafe === false){
            return 'warning-row';
          }else{
              return 'success-row'
          }
        },
        edit(){
            this.editTableShow = true
        },
        ok(){

            //this.failcnt = this.inputVal
            let data = {
                content: this.inputVal,
                id: 'USER_LOGIN_FAILED_COUNT'
            }

            this.HTTP.methods.apiPost("/sysconfig/b0002",(data)).then(res =>
            {
                if (res.code != 200) {
                    setTimeout(() => {
                        this.GLOBAL._g.shallowRefresh(this.$route.name)
                    }, 1000)
                     this.$message({
                        message: res.msg,
                        type: 'error'
                })
                } else {
                    this.tableData.body = [{loginFailCnt:this.inputVal}]

                this.$message({
                        message: res.msg,
                        type: 'success'
                })
                }
                this.editTableShow = false
            })
            this.editTableShow = false
        },
        cancel(){
            this.editTableShow = false
        },
        //缺少一种点进去链接，就发请求的listener
        query(){
            this.HTTP.methods.apiGet("/sysconfig/b0002", {}).then(res =>
            {
                if(res.code != 200){
                    console.log(res)
                    Message({
                        message: '数据异常',
                        type: 'error'
                })
                }else{
                    var value = res.data.content
                    this.inputVal = value
                    this.tableDesc = res.data.desc
                    console.log(this.tableData)
                    this.tableData.body = [{loginFailCnt:value}]
                    this.issafe = res.data.safe
                }
            }
            )
        }
    }
}
</script>


